package com.qyer.search.model;

import static com.qyer.commons.utils.CommonUtils.cast2Int;

import com.fasterxml.jackson.annotation.JsonProperty;

import javax.servlet.http.HttpServletRequest;

/**
 * User: Z J Wu Date: 2016/01/07 Time: 13:20 Package: com.qyer.search.model
 */
public class PageInfo {

  private static final int START_FROM = 1;
  private static final int DEFAULT_PAGE_SIZE = 10000;

  @JsonProperty("page_number")
  private int pageNumber;
  @JsonProperty("page_size")
  private int pageSize;

  private PageInfo() {
  }

  public PageInfo(int pageNumber, int pageSize) {
    this.pageNumber = pageNumber;
    this.pageSize = pageSize;
  }

  public static PageInfo buildPageInfo(HttpServletRequest request) {
    int pageNumber = cast2Int(request.getParameter("page_number"));
    int pageSize = cast2Int(request.getParameter("page_size"));
    return new PageInfo(pageNumber <= 0 ? START_FROM : pageNumber,
                        pageSize <= 0 ? DEFAULT_PAGE_SIZE : pageSize);
  }

  public int getPageNumber() {
    return pageNumber;
  }

  public void setPageNumber(int pageNumber) {
    this.pageNumber = pageNumber;
  }

  public int getPageSize() {
    return pageSize;
  }

  public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
  }
}
